package com.example.barcodeapp.ui.own.fragment;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.barcodeapp.MainActivity;
import com.example.barcodeapp.R;
import com.example.barcodeapp.adapter.ownadapter.AdapterXilei;
import com.example.barcodeapp.base.BaseFragment;
import com.example.barcodeapp.bean.ownbean.XiLieKeBean;
import com.example.barcodeapp.common.Constants;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.interfaces.own.IOwn;
import com.example.barcodeapp.persenter.own.OnePresenter;
import com.example.barcodeapp.ui.LoginActivity;
import com.example.barcodeapp.ui.own.activity.XiangQingActivity;
import com.example.barcodeapp.ui.own.bean.KeChengBean;
import com.example.barcodeapp.ui.own.bean.PeiLianKeBean;
import com.example.barcodeapp.utils.Show;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;


public class FragmentXilei extends BaseFragment<IOwn.Persenter2> implements IOwn.View2 {


    @BindView(R.id.xilei_rv)
    RecyclerView xileiRv;
    private List<KeChengBean.DataEntity> dataEntities;
    private AdapterXilei adapterMianFei;




    @Override
    protected int getLayout() {
        return R.layout.fragment_xilei;
    }

    @Override
    protected void initView() { }

//    public void Http(int is) {
//                try {//http://new.jifengyinyue.com/api/index/kechenglist?l_id=2&lc_id=2&cc_id=&page=0&search=&uid=
//                    Log.e("dsawsdadw", "run: "+"l_id="+""+Constants.lanmuid+"&lc_id="+is );
//
//                    URL url = new URL("http://new.jifengyinyue.com/api/index/kechenglist?"+"l_id="+""+Constants.lanmuid+"&lc_id="+is+"&cc_id="+"&page=0&search=&uid=");
//
//                    Log.e("sdawdsadawd", "run: "+url );
//                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//                    int responseCode = urlConnection.getResponseCode();
//                    if (responseCode == HttpURLConnection.HTTP_OK) {
//                        InputStream inputStream = urlConnection.getInputStream();
//                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                        String line = null;
//                        StringBuffer stringBuffer = new StringBuffer();
//                        while ((line = bufferedReader.readLine()) != null) {
//                            stringBuffer.append(line);
//                        }
//                        String json = stringBuffer.toString();
//                        final Gson gson = new Gson();
//                        final KeChengBean fengzhuanglei = gson.fromJson(json, KeChengBean.class);
////                        final List<benan.DataBean> data = fengzhuanglei.getData();
//                        getActivity().runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//
//                                dataEntities = new ArrayList<>();
//                                dataEntities=fengzhuanglei.getData();
//                                Log.e("dawda", "run: "+fengzhuanglei.getData().toString() );
//                                adapterMianFei = new AdapterXilei(getActivity(),dataEntities);
//                                adapterMianFei.setClick(new AdapterXilei.IClick() {
//                                    @Override
//                                    public void click(int pos) {
//                                        //详情界面
//                                        Intent instant = new Intent(getActivity(), XiangQingActivity.class);
//                                        instant.putExtra("data","系列课程");
//                                        startActivity(instant);
//                                    }
//                                });
//                                xileiRv.setLayoutManager(new LinearLayoutManager(getActivity()));
////        xileiRv.setLayoutManager(manager);
//                                xileiRv.setAdapter(adapterMianFei);
//                                adapterMianFei.notifyDataSetChanged();
//                            }
//                        });
//
//                    }
//
//                } catch (MalformedURLException e) {
//                    e.printStackTrace();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//    }

    @Override
    protected OnePresenter initPersenter() {
        return new OnePresenter();
    }

    @Override
    protected void initData() {
        persenter.getXiLieKe2(Constants.lanmuid,Constants.zilanmuid,0);
    }

    @Override
    public void getXiLieKe2(KeChengBean xiLieKeBean) {

        dataEntities = new ArrayList<>();
        dataEntities=xiLieKeBean.getData();
        Log.e("dawda", "run: "+xiLieKeBean.getData().toString() );
        adapterMianFei = new AdapterXilei(getActivity(),dataEntities);
        adapterMianFei.setClick(new AdapterXilei.IClick() {
            @Override
            public void click(int pos) {
                //详情界面
                Intent instant = new Intent(getActivity(), XiangQingActivity.class);
                instant.putExtra("data","系列课程");
                instant.putExtra("u_id",xiLieKeBean.getData().get(pos).getId()+"");
                Constants.kechengxiangqing=xiLieKeBean.getData().get(pos).getId();
                startActivity(instant);
            }
        });
        xileiRv.setLayoutManager(new LinearLayoutManager(getActivity()));
//        xileiRv.setLayoutManager(manager);
        xileiRv.setAdapter(adapterMianFei);
        adapterMianFei.notifyDataSetChanged();

    }
}